#pragma once
#include "Singleton.h"
#include <list>

class Observer
{
public:
	virtual void AddPlayer(int iPosition, TCHAR *iNickName) {} //pos - 1..10
	virtual void RemovePlayer(int iPosition) {}
	virtual void UpdatePlayerInfo(int iPosition, int iStake, int iStack, TCHAR *Action, TCHAR *Position, bool iActive) {}
	virtual void UpdateTableInfo(int iPot, TCHAR *iCards, int iSb, int iBb) {}
	virtual void UpdateDecision(TCHAR *iDesision) {}
	virtual void UpdateTable(HWND iHwnd, int iPlayersNum, int iStack, TCHAR *iDesision) {}
};


//---------------------------------------------------------------------------------------------------------------------------------------------
class ObserverManager : public Singleton<ObserverManager>
{
	std::list<Observer *> Observers;

public:
	void AddPlayer(int iPosition, TCHAR *iNickName)
	{
		for(std::list<Observer *>::iterator i = Observers.begin(); i != Observers.end(); ++i)
		{
			(*i)->AddPlayer(iPosition, iNickName);
		}
	}


	//------------------------------------------------------------------------------------------------------------------------------------------
	virtual void RemovePlayer(int iPosition) 
	{
		//find and delete observer
		for(std::list<Observer *>::iterator i = Observers.begin(); i != Observers.end(); ++i)
		{
			(*i)->RemovePlayer(iPosition);	
		}
	}


	//------------------------------------------------------------------------------------------------------------------------------------------
	virtual void UpdatePlayerInfo(int iPosition, int iStake, int iStack, TCHAR *Action, TCHAR *Position, bool iTurns) 
	{
		//find and delete observer
		for(std::list<Observer *>::iterator i = Observers.begin(); i != Observers.end(); ++i)
		{
			(*i)->UpdatePlayerInfo(iPosition, iStake, iStack, Action, Position, iTurns);	
		}
	}


	//------------------------------------------------------------------------------------------------------------------------------------------
	virtual void UpdateTableInfo(int iPot, TCHAR *iCards, int iSb, int iBb)
	{
		//find and delete observer
		for(std::list<Observer *>::iterator i = Observers.begin(); i != Observers.end(); ++i)
		{
			(*i)->UpdateTableInfo(iPot, iCards, iSb, iBb);	
		}
	}


	//------------------------------------------------------------------------------------------------------------------------------------------
	virtual void UpdateDecision(TCHAR *iDesision)
	{
		//find and delete observer
		for(std::list<Observer *>::iterator i = Observers.begin(); i != Observers.end(); ++i)
		{
			(*i)->UpdateDecision(iDesision);	
		}
	}


	//------------------------------------------------------------------------------------------------------------------------------------------
	virtual void UpdateTable(HWND iHwnd, int iPlayersNum, int iStack, TCHAR *iDesision) 
	{
		//find and delete observer
		for(std::list<Observer *>::iterator i = Observers.begin(); i != Observers.end(); ++i)
		{
			(*i)->UpdateTable(iHwnd, iPlayersNum, iStack, iDesision);	
		}
	}


	//------------------------------------------------------------------------------------------------------------------------------------------
	void Register(Observer *iObs)
	{
		Observers.push_back(iObs);
	}


	//------------------------------------------------------------------------------------------------------------------------------------------
	void Unregister(Observer *iObs)
	{
		//find and delete observer
		for(std::list<Observer *>::iterator i = Observers.begin(); i != Observers.end(); ++i)
		{
			if(*i == iObs)
			{
				Observers.erase(i);
				return;
			}
		}
	}
};